package advancedFeatures;

import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Stream API 使用示例和案例
 */
public class StreamAPIExamples {
    public static void main(String[] args) {
        // 1. 创建 Stream 的几种方式
        System.out.println("=== 创建 Stream 的几种方式 ===");
        
        // 从集合创建
        List<String> list = Arrays.asList("Java", "Python", "C++", "JavaScript");
        Stream<String> stream1 = list.stream();
        
        // 从数组创建
        String[] array = {"Apple", "Banana", "Orange"};
        Stream<String> stream2 = Arrays.stream(array);
        
        // 使用 Stream.of() 创建
        Stream<String> stream3 = Stream.of("One", "Two", "Three");
        
        // 2. 中间操作示例
        System.out.println("\n=== 中间操作示例 ===");
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        
        // filter - 过滤
        System.out.println("过滤偶数：");
        numbers.stream()
               .filter(n -> n % 2 == 0)
               .forEach(System.out::println);
        
        // map - 转换
        System.out.println("\n数字平方：");
        numbers.stream()
               .map(n -> n * n)
               .forEach(System.out::println);
        
        // sorted - 排序
        System.out.println("\n降序排序：");
        numbers.stream()
               .sorted((a, b) -> b - a)
               .forEach(System.out::println);
        
        // 3. 终端操作示例
        System.out.println("\n=== 终端操作示例 ===");
        
        // collect - 收集结果
        List<Integer> evenSquares = numbers.stream()
                                         .filter(n -> n % 2 == 0)
                                         .map(n -> n * n)
                                         .collect(Collectors.toList());
        System.out.println("偶数平方列表：" + evenSquares);
        
        // reduce - 归约
        int sum = numbers.stream()
                        .reduce(0, (a, b) -> a + b);
        System.out.println("数字总和：" + sum);
        
        // count - 计数
        long count = numbers.stream()
                           .filter(n -> n > 5)
                           .count();
        System.out.println("大于5的数字个数：" + count);
        
        // 4. 实际应用案例
        System.out.println("\n=== 实际应用案例 ===");
        
        // 案例1：统计字符串长度
        List<String> words = Arrays.asList("Hello", "World", "Java", "Stream", "API");
        int totalLength = words.stream()
                              .mapToInt(String::length)
                              .sum();
        System.out.println("所有单词总长度：" + totalLength);
        
        // 案例2：查找最长的单词
        Optional<String> longestWord = words.stream()
                                          .max(Comparator.comparingInt(String::length));
        longestWord.ifPresent(word -> System.out.println("最长的单词：" + word));
        
        // 案例3：分组统计
        Map<Integer, List<String>> lengthGroups = words.stream()
                                                     .collect(Collectors.groupingBy(String::length));
        System.out.println("按长度分组的单词：" + lengthGroups);
        
        // 5. 并行流示例
        System.out.println("\n=== 并行流示例 ===");
        List<Integer> largeNumbers = IntStream.range(1, 1000000)
                                            .boxed()
                                            .collect(Collectors.toList());
        
        long startTime = System.currentTimeMillis();
        long sum1 = largeNumbers.stream()
                              .reduce(0, Integer::sum);
        long endTime = System.currentTimeMillis();
        System.out.println("串行流计算时间：" + (endTime - startTime) + "ms");
        
        startTime = System.currentTimeMillis();
        long sum2 = largeNumbers.parallelStream()
                              .reduce(0, Integer::sum);
        endTime = System.currentTimeMillis();
        System.out.println("并行流计算时间：" + (endTime - startTime) + "ms");
    }
} 